package cn.tedu.luban.passport.main.repository.impl;

import cn.tedu.luban.passport.main.dao.PassportDAO;
import cn.tedu.luban.passport.main.repository.PassportRepository;
import cn.tedu.luban.passport.protocol.bo.UserBO;
import cn.tedu.luban.passport.protocol.dos.UserDO;
import cn.tedu.luban.passport.protocol.param.UserRegistryParam;
import java.util.Date;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class PassportRepositoryImpl implements PassportRepository {
    @Autowired(required = false)
    private PassportDAO passportDAO;
    @Override public UserBO getUserByUsername(String username) {
        UserDO user=passportDAO.selectUserByUsername(username);
        UserBO userBO=new UserBO();
        BeanUtils.copyProperties(user,userBO);
        return userBO;
    }

    @Override public Long addNewUser(UserRegistryParam param) {
        UserDO userDO=param2do(param);
        passportDAO.insertUser(userDO);
        return userDO.getId();
    }

    @Override public Long countUserByUsername(String username) {
        return passportDAO.countUserByUsername(username);
    }

    private UserDO param2do(UserRegistryParam param) {
        UserDO userDO=new UserDO();
        BeanUtils.copyProperties(param,userDO);
        userDO.setDeleted(1);
        userDO.setEnable(1);
        userDO.setGmtCreate(new Date());
        userDO.setGmtModified(userDO.getGmtCreate());
        return userDO;
    }

}
